package cn.wolfcode.wolf2w.service.imp;

import cn.wolfcode.wolf2w.cache.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.util.AssertUtil;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.Consts;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoRepository repository;

    @Autowired
    private IUserInfoRedisService userInoRedisService;


    @Override
    public void save(UserInfo userInfo) {

        userInfo.setId(null);
        repository.save(userInfo);
    }

    @Override
    public void delete(String id) {

        repository.deleteById(id);
    }

    @Override
    public void update(UserInfo userInfo) {

        repository.save(userInfo);
    }

    @Override
    public UserInfo get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> listAll() {
        return repository.findAll();
    }

    /**
     * 手机号校验
     *
     * @param phone
     * @return
     */
    @Override
    public boolean checkPhone(String phone) {

        // 校验手机号是否存在
        UserInfo userInfo = repository.findByPhone(phone);
        // 有值就true ,无值false
        return userInfo != null;
    }

    @Override
    public void sendVerifyCode(String phone) {

        // 1.创建验证码,编辑短信
        String code = UUID.randomUUID().toString()
                .replace("-", "").substring(0, 4);

        StringBuilder sb = new StringBuilder(80);
        sb.append("您注册的验证码是:").append(code).append("请在")
                .append(Consts.VERIFY_CODE_VAI_TIME).append("分钟之内使用");
        // 2.发送短信@ToDo
        System.out.println(sb.toString());//假装发送短信

        // 3.将短信验证码缓存到redis中
        userInoRedisService.setVerifyCode(phone, code);

    }

    @Override
    public UserInfo login(String username, String password) {
        UserInfo user = repository.findByPhone(username);
        if (user == null || !password.equals(user.getPassword())) {
            throw new LogicException("账号密码错误");

        }
        return user;
    }

    @Override
    public void register(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //1.判断传入的字符串是否为空
        AssertUtil.hasLength(phone, "手机号不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");
        //2.判断两次输入的密码是否一致
        AssertUtil.isEquals(password, rpassword);
        //3.判断手机号是否符合格式
        AssertUtil.isPhone(phone);
        //4.判断手机号是否唯一
        if (this.checkPhone(phone)) {
            throw new RuntimeException("手机号码已被注册");
        }
        //判断验证是否正确(根据手机号)
        String code = userInoRedisService.getVerifyCode(phone);
        if (code == null || !code.equalsIgnoreCase(verifyCode)) {
            throw new RuntimeException("验证码输入错误");
        }
        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setPassword(password);
        userInfo.setLevel(1);
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setState(UserInfo.STATE_NORMAL);
        //保存
        this.save(userInfo);
    }
}
